package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.ProductTypeAddDTO;
import org.jsola.hr.dto.ProductTypeUpdateDTO;
import org.jsola.hr.entity.ProductTypeDO;
import org.jsola.hr.query.ProductTypeQuery;
import org.jsola.hr.service.IProductTypeService;
import org.jsola.hr.vo.ProductTypeListVO;
import org.jsola.hr.vo.ProductTypeVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;
import javax.validation.Valid;


/**
 * 产品分类
 *
 * @author zhr
 */
@RestController("hrProductTypeController")
@RequestMapping(HrConstants.API_PATH + "/v1/productTypes")
public class ProductTypeController implements IProductTypeControllerAPI {

    @Autowired
    private IProductTypeService productTypeService;

    @Override
    @PostMapping
    public Result<ProductTypeVO> save(@Valid @RequestBody ProductTypeAddDTO productTypeAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增产品分类权限校验

        return Result.success(
                productTypeService.save(productTypeAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody ProductTypeUpdateDTO productTypeUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改产品分类权限校验

        int count = productTypeService.update(productTypeUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }


    @Override
    @DeleteMapping(value = "/{productTypeId}")
    public Result<Integer> delete(@PathVariable String productTypeId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除产品分类权限校验

        int count = productTypeService.deleteByIds(tokenUser, productTypeId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] productTypeIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除产品分类权限校验

        int count = productTypeService.deleteByIds(tokenUser, productTypeIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{productTypeId}")
    public Result<ProductTypeVO> findById(@PathVariable String productTypeId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看产品分类权限校验

        return Result.success(
                productTypeService.selectById(productTypeId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<ProductTypeListVO>> listAll(@Valid ProductTypeQuery productTypeQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看产品分类权限校验

        return Result.success(
                productTypeService.select(productTypeQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<ProductTypeListVO>> page(@Valid ProductTypeQuery productTypeQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                productTypeService.selectPage(productTypeQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping("/selectBySite")
    public Result<List<ProductTypeListVO>> selectBySite() {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        List<ProductTypeDO> productTypeDOList = productTypeService.selectBySite(tokenUser.getSiteId());
        return Result.success(
                productTypeDOList.parallelStream().map(productTypeDO
                        -> productTypeDO.to(ProductTypeListVO.class)).parallel().collect(Collectors.toList())
        );
    }


}

